<template>
  <view
    class="use-mask pos-f pos-full overflow-hidden"
  :class="[show ? 'use-mask-show' : '']"
    :style="{ ...maskStyle }"
    @touchmove.stop.prevent
    @tap="click"
    @contextmenu.prevent
  >
    <slot />
  </view>
</template>

<script lang="ts">
import { defineComponent, reactive, computed, toRefs } from 'vue';

interface Props {
  show: boolean;
  zIndex: number | string;
  zoom: boolean;
  duration: number | string;
  maskClickAble: boolean;
}

export default defineComponent({
  props: {
    // 是否显示遮罩
    show: {
      type: Boolean,
      default: false
    },
    // 层级 z-index
    zIndex: {
      type: [Number, String],
      default: '10000'
    },
    // 遮罩的动画样式， 是否使用使用 zoom 进行 scale 进行缩放
    zoom: {
      type: Boolean,
      default: true
    },
    // 遮罩的过渡时间，单位为 ms
    duration: {
      type: [Number, String],
      default: 300
    },
    // 是否可以通过点击遮罩进行关闭
    maskClickAble: {
      type: Boolean,
      default: true
    }
  },
  setup(props: Props, { emit }) {
    const maskStyle = computed(() => {
      let style = {
        transition: `all ${Number(props.duration) / 1000}s ease-in-out`,
        zIndex: Number(props.zIndex)
      };
      if (props.show) {
        style.zIndex = Number(props.zIndex);
      } else {
        style.zIndex = -1;
      }
      // 缩放
      if (props.zoom) {
        style.transform ='scale(1.2, 1.2)';
      }
      return style;
    });

    const click = () => {
      if (props.maskClickAble) {
        emit('click');
      }
    };

    return {
      maskStyle,
      click
    };
  }
});
</script>

<style lang="scss">
.use-mask {
  opacity: 0;
  background-color: rgba(0, 0, 0, 0.6);
}

.use-mask-show {
  opacity: 1;
  visibility: visible;
  transform: scale(1);
}
</style>